// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';

// ============ 权限管理 API ============

/** 获取权限列表 GET /api/permissions */
export async function getPermissions(
  params?: {
    current?: number;
    pageSize?: number;
    type?: string;
    status?: string;
  },
  options?: { [key: string]: any },
) {
  return request<API.PermissionList>('/api/permissions', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 创建权限 POST /api/permissions */
export async function createPermission(
  body: Omit<API.Permission, 'id' | 'createdAt' | 'updatedAt'>,
  options?: { [key: string]: any },
) {
  return request<API.Permission>('/api/permissions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 更新权限 PUT /api/permissions/:id */
export async function updatePermission(
  id: string,
  body: Partial<API.Permission>,
  options?: { [key: string]: any },
) {
  return request<API.Permission>(`/api/permissions/${id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 删除权限 DELETE /api/permissions/:id */
export async function deletePermission(
  id: string,
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/api/permissions/${id}`, {
    method: 'DELETE',
    ...(options || {}),
  });
}

/** 获取权限树 GET /api/permissions/tree */
export async function getPermissionTree(options?: { [key: string]: any }) {
  return request<API.Permission[]>('/api/permissions/tree', {
    method: 'GET',
    ...(options || {}),
  });
}

// ============ 角色管理 API ============

/** 获取角色列表 GET /api/roles */
export async function getRoles(
  params?: {
    current?: number;
    pageSize?: number;
    name?: string;
    status?: string;
  },
  options?: { [key: string]: any },
) {
  return request<API.RoleList>('/api/roles', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 创建角色 POST /api/roles */
export async function createRole(
  body: API.CreateRoleParams,
  options?: { [key: string]: any },
) {
  return request<API.Role>('/api/roles', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 更新角色 PUT /api/roles/:id */
export async function updateRole(
  body: API.UpdateRoleParams,
  options?: { [key: string]: any },
) {
  return request<API.Role>(`/api/roles/${body.id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 删除角色 DELETE /api/roles/:id */
export async function deleteRole(
  id: string,
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/api/roles/${id}`, {
    method: 'DELETE',
    ...(options || {}),
  });
}

/** 获取角色详情 GET /api/roles/:id */
export async function getRole(
  id: string,
  options?: { [key: string]: any },
) {
  return request<API.Role>(`/api/roles/${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

// ============ 用户角色管理 API ============

/** 获取用户列表（含角色） GET /api/users */
export async function getUsers(
  params?: {
    current?: number;
    pageSize?: number;
    name?: string;
    roleId?: string;
  },
  options?: { [key: string]: any },
) {
  return request<API.UserList>('/api/users', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 创建用户 POST /api/users */
export async function createUser(
  body: API.CreateUserParams,
  options?: { [key: string]: any },
) {
  return request<API.UserWithRoles>('/api/users', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 更新用户 PUT /api/users/:id */
export async function updateUser(
  body: API.UpdateUserParams,
  options?: { [key: string]: any },
) {
  return request<API.UserWithRoles>(`/api/users/${body.id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 删除用户 DELETE /api/users/:id */
export async function deleteUser(
  id: string,
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/api/users/${id}`, {
    method: 'DELETE',
    ...(options || {}),
  });
}

/** 分配用户角色 POST /api/users/:id/roles */
export async function assignUserRoles(
  userId: string,
  roleIds: string[],
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/api/users/${userId}/roles`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: { roleIds },
    ...(options || {}),
  });
}

/** 获取用户权限 GET /api/users/:id/permissions */
export async function getUserPermissions(
  userId: string,
  options?: { [key: string]: any },
) {
  return request<API.Permission[]>(`/api/users/${userId}/permissions`, {
    method: 'GET',
    ...(options || {}),
  });
}
